package com.oj.core.entity;

import com.oj.api.core.domain.ProblemInfo;
import com.oj.api.core.domain.ProblemSetCategoryInfo;
import com.oj.api.core.domain.ProblemSetInfo;
import com.oj.common.data.BaseEntity;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 题目集实体
 * Created by Bing on 2018/5/4.
 */
@Entity(name = "CORE_PROBLEM_SET_Category")
@Table(indexes = {
        @Index(name = "core_idx_code_problem_set_category", unique = true, columnList = "code"),
        @Index(name = "pms_idx_updatetime_problem_set_category", columnList = "UPDATE_TIME")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ProblemSetCategoryEntity extends BaseEntity<ProblemSetCategoryInfo>{

    /**
     * 单位
     */
    private String company;
    /**
     * 类型
     */
    private Integer type;


    /**
     * 题目集集合
     */
    @OneToMany(mappedBy = "problemSetCategoryEntity",cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
    @OrderBy(value = "company")
    private Set<ProblemSetEntity> problemSets;

    @Override
    protected ProblemSetCategoryInfo createInfo() {
        return new ProblemSetCategoryInfo();
    }

    @Override
    public ProblemSetCategoryInfo buildInfo(ProblemSetCategoryInfo problemSetCategoryInfo) {
        super.buildInfo(problemSetCategoryInfo);
        List<ProblemSetInfo> lines = new ArrayList<>();
        if(this.getProblemSets()!=null){
            this.getProblemSets().forEach(a->{
                lines.add(a.buildInfo());
            });
        }
        problemSetCategoryInfo.setProblemSets(lines);
        return problemSetCategoryInfo;
    }

    public void addProblemSet(ProblemSetEntity problemSetEntity){
        if(problemSets==null){
            problemSets = new HashSet<>();
        }
        problemSetEntity.setProblemSetCategoryEntity(this);
        problemSets.add(problemSetEntity);
    }
}
